﻿<template>
	<transition name="el-zoom-in-center">
		<div class="JNPF-preview-main">
			<div class="JNPF-common-page-header">
				<el-page-header @back="goBack" :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"/>
				<div class="options">
					<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
					<el-button @click="goBack">取 消</el-button>
				</div>
			</div>
			<el-row :gutter="15"  class="main" :style="{margin: '0 auto',width:'100%'}">
				<el-form ref="elForm" :model="dataForm" size="medium" label-width="111px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="24" >
						<el-form-item label="产品编号" prop="encode">
							<el-input v-model="dataForm.encode" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="产品名称" prop="name">
							<el-input v-model="dataForm.name" placeholder="请输入" clearable required :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="产品品牌" prop="brand">
							<el-input v-model="dataForm.brand" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="产品类型" prop="type">
							<el-select v-model="dataForm.type" placeholder="请选择" clearable required :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in typeOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="产品单位" prop="unit">
							<el-select v-model="dataForm.unit" placeholder="请选择" clearable required :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in unitOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="最低销售价" prop="lowerPrice">
							<el-input-number v-model="dataForm.lowerPrice" placeholder="数字文本" required :step="100" controls-position="right" >
							</el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="市场参考价" prop="proposedPrice">
							<el-input-number v-model="dataForm.proposedPrice" placeholder="数字文本" required :step="100" controls-position="right" >
							</el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="库存预警下限" prop="subminv">
							<el-input-number v-model="dataForm.subminv" placeholder="数字文本" required :step="1" >
							</el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="库存预警上限" prop="submaxv">
							<el-input-number v-model="dataForm.submaxv" placeholder="数字文本" required :step="1" >
							</el-input-number>
						</el-form-item>
					</el-col>
					<el-col :span="24" >
						<el-form-item label="产品图片" prop="photos">
							<JNPF-UploadImg v-model="dataForm.photos" :fileSize="2" sizeUnit="MB" :limit="9" >
							</JNPF-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="24" >
						<el-form-item label="备注" prop="remark">
							<el-input v-model="dataForm.remark" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建用户" prop="creatorUserId">
							<el-input v-model="dataForm.creatorUserId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建时间" prop="creatorTime">
							<el-input v-model="dataForm.creatorTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="修改用户" prop="lastModifyUserId">
							<el-input v-model="dataForm.lastModifyUserId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="修改时间" prop="lastModifyTime">
							<el-input v-model="dataForm.lastModifyTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
		</div>
	</transition>
</template>
<script>
	import request from '@/utils/request'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					encode:undefined,
					name:undefined,
					brand:undefined,
					type:undefined,
					unit:undefined,
					lowerPrice:0,
					proposedPrice:0,
					subminv:0,
					submaxv:0,
					photos:[],
					remark:undefined,
					creatorUserId:undefined,
					creatorTime:undefined,
					lastModifyUserId:undefined,
					lastModifyTime:undefined,
				},
				rules: {
					name:[
						{
							required:true,
							message:'请输入产品名称',
							trigger:'blur'
						},
					],
					type:[
						{
							required:true,
							message:'请输入产品类型',
							trigger:'change'
						},
					],
					unit:[
						{
							required:true,
							message:'请输入产品单位',
							trigger:'change'
						},
					],
					lowerPrice:[
						{
							required:true,
							message:'请输入最低销售价',
							trigger:["blur","change"]
						},
					],
					proposedPrice:[
						{
							required:true,
							message:'请输入市场参考价',
							trigger:["blur","change"]
						},
					],
					subminv:[
						{
							required:true,
							message:'请输入库存预警下限',
							trigger:["blur","change"]
						},
					],
					submaxv:[
						{
							required:true,
							message:'请输入库存预警上限',
							trigger:["blur","change"]
						},
					],
				},
				typeOptions : [],
				unitOptions : [],
			}
		},
		computed: {},
        watch: {},
        created() {
			this.gettypeOptions();
			this.getunitOptions();
		},
		mounted() {
        },
		methods: {
			gettypeOptions(){
				getDictionaryDataSelector('98767290630840f2926c1edea9c5b494').then(res => {
					this.typeOptions = res.data.list
				});
			},
			getunitOptions(){
				getDictionaryDataSelector('d4d4cc4401064c34a81099a76f778dc3').then(res => {
					this.unitOptions = res.data.list
				});
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				this.dataForm.id = id || 0;
                this.visible = true;
                this.isDetail = isDetail || false;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/SubDev/ZyOaProduct/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
						})
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/SubDev/ZyOaProduct`,
                                method: 'post',
                                data: this.dataForm,
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            })
                        } else {
                            request({
                                url: '/api/SubDev/ZyOaProduct/' + this.dataForm.id,
                                method: 'PUT',
                                data: this.dataForm
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refresh', true)
                                    }
                                })
                            })
                        }
                    }
                })
			},
		}
	}
</script>
